<?php
namespace Admin\Controller;
use Think\Controller;
//备份用
defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
class CommonController extends Controller
{
    //如果未登录，就跳转到登录页面
    public function _initialize($params)
    {
        if(session("shop_id") !== "0"){
            $this->redirect('Login/index');
            return false;
        }
        $this->get_config();
        $this->get_nav();
        $c = strtolower(CONTROLLER_NAME);
        $a = strtolower(ACTION_NAME);
        $url = $c .'/'. $a;
        // 超级管理员
        $login_role = session("role_id");
        if($login_role == 1){
            return true;
        }
        // $params 参数请保证是全小写字母组成的以为数据
        // 例如 $params = array('admin/index' ,'system/index');
        if(in_array($a ,$params)){
            return true;
        }
        // 查询自己拥有的所有权限
        $my_permissions = M("permission")->where("role_id = ".session("role_id"))->select();
        // 未分配任何权限
        if($my_permissions){
            foreach($my_permissions as $val){
                $urls[] = $val['url'];
            }
            if(in_array($url ,$urls)){
                return true;
            }
        }
        // 错过了前面所有村
        $this->error("您没有此权限！" , $_SERVER['HTTP_REFERER'],1);
        return false;
    }

    // 加载默认配置
    public function get_config()
    {
        $configs = M("config")->where("conf_type = 'system'")->select();
        foreach($configs as $key => $config){
            $newConfig[$config['conf_key']] = $config['conf_value'];
        }
        $this->assign("config" ,$newConfig);
        $login_msg = M("manager")->where("id = %d",array(session("manager_id")))->field("last_login,last_ip")->find();
        $this->assign("login_msg" ,$login_msg);
    }

    // 读取导航
    public function get_nav()
    {
        $login_role_id = session("role_id");
        $sys_nav = S("sys_nav_" .$login_role_id);
        $shop_nav = S("shop_nav_" .$login_role_id);
        $otr_nav = S("otr_nav_" .$login_role_id);
        $pln_nav = S("pln_nav_" .$login_role_id);
        if(empty($sys_nav) && empty($shop_nav) && empty($otr_nav) && empty($pln_nav))
        {
            if($login_role_id == 1){
                // 系统导航
                $s_navs = M("navigation")->where("nav_status = 1 AND menu_code = 'system' AND parent_id = 0")->order("nav_sort ASC")->select();
                foreach($s_navs as $key_s => $val_s){
                    $s_navs[$key_s]["sub_nav"] = M("navigation")->where("nav_status = 1 AND menu_code = 'system' AND parent_id = %d" ,array($val_s['nav_id']))->order("nav_sort ASC")->select();
                }
                // 商城导航
                $p_navs = M("navigation")->where("nav_status = 1 AND menu_code = 'shop' AND parent_id = 0")->order("nav_sort ASC")->select();
                foreach($p_navs as $key_p => $val_p){
                    $p_navs[$key_p]["sub_nav"] = M("navigation")->where("nav_status = 1 AND menu_code = 'shop' AND parent_id = %d" ,array($val_p['nav_id']))->order("nav_sort ASC")->select();
                }
                // 其他导航
                $o_navs = M("navigation")->where("nav_status = 1 AND menu_code = 'other' AND parent_id = 0")->order("nav_sort ASC")->select();
                foreach($o_navs as $key_o => $val_o){
                    $o_navs[$key_o]["sub_nav"] = M("navigation")->where("nav_status = 1 AND menu_code = 'other' AND parent_id = %d" ,array($val_o['nav_id']))->order("nav_sort ASC")->select();
                }
                // 插件
                $g_navs = M("navigation")->where("nav_status = 1 AND menu_code = 'plugins' AND parent_id = 0")->order("nav_sort ASC")->select();
                foreach($g_navs as $key_g => $val_g){
                    $g_navs[$key_g]["sub_nav"] = M("navigation")->where("nav_status = 1 AND menu_code = 'plugins' AND parent_id = %d" ,array($val_o['nav_id']))->order("nav_sort ASC")->select();
                }
            }else{
                $urls = array();
                // 先查询当前用户权限
                $permissions = M("permission")->where("role_id = %d",array($login_role_id))->field("url")->select();
                foreach($permissions as $pms){
                    $pms_url = explode("/" ,$pms['url']);
                    $auth_url = M("navigation")->where("nav_c = '%s' AND nav_a = '%s'" ,array($pms_url[0] ,$pms_url[1]))->order("nav_sort ASC")->find();
                    if($auth_url){
                        $urls[] = $auth_url;
                    }
                }
                $isst = array();
                foreach($urls as $url){
                    if(!in_array($url["parent_id"] ,$isst)){
                        $isst[] = $url["parent_id"];
                    }
                }
                $ps_navs = M("navigation")->where("nav_status = 1 AND nav_id IN (".implode("," ,$isst).")")->order("nav_sort ASC")->select();
                foreach($ps_navs as $key => $ps)
                {
                    foreach($urls as $u){
                        if($ps["nav_id"] == $u["parent_id"]){
                            $ps_navs[$key]["sub_nav"][] = $u;
                        }
                    }
                }
                foreach($ps_navs as $ps_val){
                    switch($ps_val["menu_code"]){
                        case "system":
                            $s_navs[] = $ps_val;
                            break;
                        case "shop":
                            $p_navs[] = $ps_val;
                            break;
                        case "other":
                            $o_navs[] = $ps_val;
                            break;
                        case "plugins":
                            $g_navs[] = $ps_val;
                            break;
                    }
                }
            }
            S("sys_nav_" .$login_role_id,$s_navs);
            S("shop_nav_" .$login_role_id ,$p_navs);
            S("otr_nav_" .$login_role_id , $o_navs);
            S("pln_nav_" .$login_role_id , $g_navs);
        }
        $this->assign("s_navs" ,$sys_nav);
        $this->assign("p_navs" ,$shop_nav);
        $this->assign("o_navs" ,$otr_nav);
        $this->assign("g_navs" ,$pln_nav);
    }
}